//
//  TestViewController.swift
//  swiftDemo
//
//  Created by Gerry on 2021/10/30.
//

import UIKit


class TestViewController: BaseViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        self.parseJson()
    }
    

    @objc func parseJson() {
//        let str = self.getStr()
//        let users = NSArray.yy_modelArray(with: UserModel.self, json: str) as! [UserModel]
//        print(users)
        let str = self.getJsonString()
        let data = self.getDictionaryFromJSONString(jsonString: str as String)
        // convert to dictionary
        let citys = data["citys"] as! NSArray
        let cityList = NSArray.yy_modelArray(with: CityListModel.self, json: citys) as! [CityListModel]
        print(cityList)
    }
    
    func getDictionaryFromJSONString(jsonString:String) ->NSDictionary{
        let jsonData:Data = jsonString.data(using: .utf8)!
        let dict = try? JSONSerialization.jsonObject(with: jsonData, options: .mutableContainers)
        if dict != nil {
            return dict as! NSDictionary
        }
        return NSDictionary()
    }
     
    
    func getJSONStringFromDictionary(dictionary:NSDictionary) -> NSString {
        if (!JSONSerialization.isValidJSONObject(dictionary)) {
            print("无法解析出JSONString")
            return ""
        }
        let data : NSData! = try? JSONSerialization.data(withJSONObject: dictionary, options: []) as NSData?
        let JSONString = NSString(data:data as Data,encoding: String.Encoding.utf8.rawValue)
        return JSONString!
    }
    
    
    func getStr()->NSString {
        return """
        [{
                "id":1,
                "name":"gerry"
            },
            {
                "id":2,
                "name":"tony"
            }]
        
        """
    }
    
    func getJsonString()->NSString {
        let str = """
{
        "citys": [{
            "id": 9,
            "code": "100000",
            "name": "北京市",
            "baiduCode": "131",
            "gaodeCode": "",
            "tengxunCode": "",
            "parentId": 8,
            "level": 2,
            "areas": [{
                "id": 10,
                "code": "100020",
                "name": "朝阳区",
                "baiduCode": "",
                "gaodeCode": "",
                "tengxunCode": "",
                "parentId": 9,
                "level": 3,
                "areas": null
            }, {
                "id": 13,
                "code": "100089",
                "name": "海淀区",
                "baiduCode": null,
                "gaodeCode": null,
                "tengxunCode": null,
                "parentId": 9,
                "level": 3,
                "areas": null
            }]
        }, {
            "id": 12,
            "code": "300000",
            "name": "天津市",
            "baiduCode": null,
            "gaodeCode": null,
            "tengxunCode": null,
            "parentId": 11,
            "level": 2,
            "areas": null
        }]
    }
"""
        
        return str as NSString
    }

}
